package com.doctorpda.compresser;

import java.io.File;
import java.io.IOException;
import java.util.Date;
 
/**
 * 工具类：利用yuicompressor来对js、css文件进行压缩
 * 
 * @author YEDWARD
 * 
 */
public class CompressorUtil {
    /**
     * main方法
     * 
     * @param args
     */
    public static void main(String[] args) {
        /**
         * 压缩时，将这里的：
         * yuiPath替换成你机器上yuicompress文件的路径； 
         * filePath替换成要压缩的文件或文件夹路径；
         * fileType替换成要压缩的文件类型(扩展名)，只能是js、css两种类型； 
         * encoding替换成你要读取文件的编码格式
         */
        String yuiPath = "C:/Users/Administrator/git/camel_cdn/lib/yuicompressor-2.4.8.jar";
        String filePath = "D:/api";
        String fileType = "js";
        String encoding = "utf-8";
 
        StringBuilder sb = new StringBuilder();
        File file = new File(filePath);
        if (fileType.equalsIgnoreCase("JS")) {
            compressJS(yuiPath, sb, file, encoding);
        }
        if (fileType.equalsIgnoreCase("CSS")) {
            compressCSS(yuiPath, sb, file, encoding);
        }
        String[] res = sb.toString().split("\n");
        Runtime runTime = Runtime.getRuntime();
        Date startTime = new Date();
        Long count = 0L;
        for (String cmd : res) {
            if (!cmd.trim().equals("")) {
                System.out.println(cmd);
                try {
                    runTime.exec(cmd);
                    count++;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Date endTime = new Date();
        Long cost = endTime.getTime() - startTime.getTime();
        System.out.println(fileType.toUpperCase() + "压缩完成，耗时：" + cost + "ms，共压缩文件个数：" + count);
    }
 
    /**
     * 压缩JS文件
     */
    public static void compressJS(String yuiPath, StringBuilder sb, File f, String encoding) {
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            // 如果某个文件夹是空文件夹，则跳过
            if (files == null) {
                return;
            }
            for (File file : files) {
                compressJS(yuiPath, sb, file, encoding);
            }
        } else {
            String fileName = f.getName();
            if (fileName.endsWith(".js") && !fileName.endsWith("-min.js")) {
                sb.append("java -jar ");
                sb.append(yuiPath);
                sb.append(" --type js --charset ");
                sb.append(encoding + " " + f.getPath());
                sb.append(" -o " + f.getPath().replace(".js", "-min.js"));
                sb.append("\n");
            }
        }
    }
 
    /**
     * 压缩CSS文件
     */
    public static void compressCSS(String yuiPath, StringBuilder sb, File f, String encoding) {
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            // 如果某个文件夹是空文件夹，则跳过
            if (files == null) {
                return;
            }
            for (File file : files) {
                compressCSS(yuiPath, sb, file, encoding);
            }
        } else {
            String fileName = f.getName();
            if (fileName.endsWith(".css") && !fileName.endsWith("-min.css")) {
                sb.append("java -jar ");
                sb.append(yuiPath);
                sb.append(" --type css --charset ");
                sb.append(encoding + " " + f.getPath());
                sb.append(" -o " + f.getPath().replace(".css", "-min.css"));
                sb.append("\n");
            }
        }
    }
}